package com.wangchao.administrator.zenmechi.ui.fragment.buyFragment;

import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by cbb on 2016/10/18.
 * Email: 1023151857@qq.com
 */
public class buyModle implements BuyContract.Modle {
    private List<BasketBean> list;

    @Override
    public void queryData(String market, Subscriber<List<BasketBean>> marketList) {
        Observable.create(new Observable.OnSubscribe<List<BasketBean>>() {
            @Override
            public void call(Subscriber<? super List<BasketBean>> subscriber) {
                BmobQuery<BasketBean> query = new BmobQuery<BasketBean>();
                query.addWhereNotEqualTo("name","bmob");
//                query.setLimit(50);//设置查询的条数
                query.findObjects(new FindListener<BasketBean>() {
                    @Override
                    public void done(List<BasketBean> object, BmobException e) {
                        if(e==null){
                           list=object;
                        }else{
                            Log.i("bmob","失败："+e.getMessage()+","+e.getErrorCode());
                        }
                    }
                });
                subscriber.onNext(list);
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(marketList);
    }

    @Override
    public void upData(int position,int type) {
        int count;
        if(type==0){
             count= list.get(position).getListGoods().get(0).getCounts()+1;
        }else{
            count =list.get(position).getListGoods().get(0).getCounts()-1;
        }
        if (count==0){
            list.get(position).delete(new UpdateListener() {
                @Override
                public void done(BmobException e) {
                    Log.e("bmob","删除成功");
                }
            });
        }
        list.get(position).getListGoods().get(0).setCounts(count);
        list.get(position).update(list.get(position).getObjectId(), new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if(e==null){
                }
            }
        });
    }

    @Override
    public void deleteData(String name) {

    }

    @Override
    public void saveData(String market, String url, String name, String price, int count) {
        List<GoodsBean> listgoods=new ArrayList<>();
        BasketBean basket=new BasketBean();
        GoodsBean goods=new GoodsBean();

        goods.setGoods_name(name);
        goods.setPrice(price);
        goods.setPic(url);
        goods.setCounts(count);
        listgoods.add(goods);
        basket.setName("bmob");
        basket.setMarket_name(market);
        basket.setListGoods(listgoods);
        basket.save(new SaveListener<String>() {
            @Override
            public void done(String s, BmobException e) {
                if(e==null){
                    Log.e("bomb","存储成功");
                }
            }
        });
    }
}
